var express = require('express');
var router = express.Router();
var sql = require('./../tool/sql');

/* GET users listing. */
//查询
router.get('/', function(req, res, next) {
  sql.find('pushup', {}, {_id: 0}).then(data => {
    console.log(data)
    data.sort((a,b) => {
      return b.run - a.run;
    })
    res.render('pushup', {
      result: data
    })
  }).catch(err => console.log(err))
});

//添加
router.get('/add', function(req, res, next) {
  const insertData = req.query;  //路由的名字和个数
  sql.insert('pushup', insertData).then(() => {
    res.redirect('/pushup')
  }).catch(err => console.log(err))
})

//删除
router.get('/delete',function(req, res, next) {
  const deleteData = req.query;
  sql.remove('pushup',deleteData).then(() => {
    res.redirect('/pushup')
  }).catch(err => console.log(err))
})

//修改
router.get('/updateOne', function(req, res, next) {
  const obj = req.query; // req.query  --- {username:'', run: 2}
  const whereData = { username: obj.username };
  const updateData = {
    $set: obj
  }
  sql.updateOne('pushup', whereData, updateData).then(() => {
    res.redirect('/pushup')
  }).catch(err => console.log(err))
});
module.exports = router;